<template>
  <div class="page-container">
	<!--工具栏-->
	<div class="toolbar" style="float:left;padding-top:10px;padding-left:5px;">
		<el-form :inline="true" :model="filters" :size="size">
			<!-- <el-form-item>
				<el-input v-model="filters.name" placeholder="名称"></el-input>
			</el-form-item>
			<el-form-item>
				<kt-button icon="fa fa-search" :label="$t('action.search')" perms="sys:menu:view" type="primary" @click="findTreeData(null)"/>
			</el-form-item> -->
			<el-form-item>
				<kt-button icon="fa fa-plus" :label="$t('action.add')" perms="sys:menu:save" type="primary" @click="handleAddOrUpdate('', 'add')"/>
			</el-form-item>
		</el-form>
	</div>
	<!--表格树内容栏-->
    <!-- <el-table :data="tableTreeDdata" stripe size="mini" style="width: 100%;"
      rowKey="id" v-loading="loading" element-loading-text="$t('action.loading')"> -->
    <el-table
      :data="tableTreeDdata"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      v-loading="loading"
      border
      :size="size"
      element-loading-text="$t('action.loading')"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column header-align="center" prop="menuTitle" label="名称" min-width="20%">
      </el-table-column>
      <el-table-column header-align="center" align="center" label="图标" min-width="10%">
        <template slot-scope="scope">
          <!-- <i :class="scope.row.icon || ''"></i> -->
          <icon-svg :name="scope.row.icon || ''"></icon-svg>
        </template>
      </el-table-column>
      <el-table-column prop="menuType" header-align="center" align="center" label="类型" min-width="10%">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.menuType === '0'" size="small">目录</el-tag>
          <el-tag v-else-if="scope.row.menuType === '1'" size="small" type="success">菜单</el-tag>
          <el-tag v-else-if="scope.row.menuType === '2'" size="small" type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="pathUrl" header-align="center" align="center" min-width="25%"
        :show-overflow-tooltip="true" label="菜单URL">
      </el-table-column>
      <el-table-column
        prop="perms" header-align="center" align="center" width="150"
        :show-overflow-tooltip="true" label="授权标识">
      </el-table-column>
      <el-table-column
        fixed="right" header-align="center" align="center" min-width="25%" :label="$t('action.operation')">
        <template slot-scope="scope">
          <kt-button v-if="scope.row.menuType !== '2'" icon="fa fa-plus" :label="$t('action.add')" perms="sys:menu:save" type="primary" @click="handleAddOrUpdate(scope.row, 'add')"/>
          <kt-button icon="fa fa-edit" :label="$t('action.edit')" perms="sys:menu:update" @click="handleAddOrUpdate(scope.row, 'update')"/>
          <kt-button icon="fa fa-trash" :label="$t('action.delete')" perms="sys:menu:delete" type="danger" @click="handleDelete(scope.row)"/>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增修改界面 -->
    <add-or-update ref="addorupdateForm" :size="size" @refreshTable="findTreeData"/>
  </div>
</template>

<script>
import KtButton from "@/views/Core/KtButton"
import { menuSave, getMenu, getMenuData, getMenuInfo, menuDelete } from '@/api/auth/menu'
import AddOrUpdate from './components/addorupdate' 
export default {
  components: {
    KtButton,
    AddOrUpdate
  },
  data() {
    return {
      size: window.SITE_CONFIG.tableSize,
      loading: false,
      filters: {
        name: ""
      },
      tableTreeDdata: [],
      parentName: ''
    };
  },
  mounted() {
    this.findTreeData()
  },
  methods: {
    // 获取数据
    findTreeData: function() {
      this.loading = true;
      getMenu().then(res => {
        this.tableTreeDdata = res.data;
        this.loading = false;
      });
    },
    // 显示新增/修改界面
    handleAddOrUpdate(row, type) {
      const that = this
      this.$nextTick(() => {
        that.$refs.addorupdateForm.init(row, type)
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm("确认删除选中记录吗？", "提示", {
        type: "warning"
      }).then(() => {
        // let params = this.getDeleteIds([], row);
        let params = { id: row.id }
        menuDelete(params).then(res => {
          this.findTreeData();
        });
      });
    },
    // 获取删除的包含子菜单的id列表
    getDeleteIds(ids, row) {
      ids.push({ id: row.id });
      if (row.children != null) {
        for (let i = 0, len = row.children.length; i < len; i++) {
          this.getDeleteIds(ids, row.children[i]);
        }
      }
      return ids;
    },
    // 菜单树选中
    handleTreeSelectChange(data, node) {
      this.dataForm.parentId = data.id;
      this.parentName = data.menuTitle;
    }
  }
};
</script>

<style lang="scss">
  .mod-menu__icon-list {
    max-height: 380px;
    padding: 0;
    margin: -8px 0 0 -8px;

    > .el-button {
      padding: 8px;
      margin: 8px 0 0 8px;

      > span {
        display: inline-block;
        vertical-align: middle;
        width: 18px;
        height: 18px;
        font-size: 18px;
      }

    }
  }

  .icon-list__tips {
    font-size: 18px;
    text-align: center;
    color: #e6a23c;
    cursor: pointer;
  }
</style>
